<template>
  <view></view>
</template>

<script>
import {login} from "@/utils/wechart";
import BaseService from "@/api/BaseService";
import CacheUtils from "@/utils/CacheUtils";

export default {
  onLoad({url}) {
    this.initLogin(url);
  },
  methods: {
    async initLogin(url) {
      let params = new URLSearchParams(location.search);
      let code = params.get("code");
      let state = params.get("state");
      console.log(state);
      if (code) {
        try {
          let res = await BaseService.baseLogin(code);
          console.log("/////////", res)
          if (res) {
            CacheUtils.setCache("userInfo", res);
            CacheUtils.setCache("openid", res.openid);
            if (res.token) {
              CacheUtils.setCache("sessionId", res.token);
              location.href = state || "/pages/index/index";
            } else {
              console.log("5555")
            }
          }
        } catch (err) {
          console.log("<========>", err)
        }
      } else {
        login({state: url});
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
